﻿/*
 * 文件名称：
 * 程序员：huangsb
 * 创建时间：2008/05/10
 * 
 * 功能说明：邮箱配置
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Config_EmailConfig : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        if (!IsPostBack)
        {
            //string xmlPath = Server.MapPath(Whir.Common.Settings.GetString("XmlPath") + "EmailConfig.xml");
            string xmlPath = Server.MapPath("~/"+Whir.Common.Settings.GetString("XmlPath") + "EmailConfig.xml");

            Type type = typeof(Whir.Config.Model.EmailConfig);
            Whir.Config.Model.EmailConfig model = (Whir.Config.Model.EmailConfig)Whir.Common.XmlHelper.DeserializeObject(xmlPath, type);         //反序列化

            this.txtEmail.Text = model.email;
            this.txtSMTP.Text = model.SMTP;
            this.txtPort.Text = model.port;
            this.cbSSL.Checked = (model.SSL == "1") ? true : false;
            this.hf1.Value = model.validateWay;
            this.txtUserName.Text = model.userName;
            this.txtPassword.Text = model.password;
        }
    }

    //修改
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Whir.Config.Model.EmailConfig model = new Whir.Config.Model.EmailConfig();

        model.email = this.txtEmail.Text;
        model.SMTP = this.txtSMTP.Text;
        model.port = this.txtPort.Text;
        model.SSL = (this.cbSSL.Checked) ? "1" : "0";

        if (Request.Form["rValidateWay"] != null)
        {
            model.validateWay = Request.Form["rValidateWay"].ToString();

            if (model.validateWay == "2")
            {
                model.userName = this.txtUserName.Text;
                model.password = this.txtPassword.Text;
            }
            else
            {
                model.userName = "";
                model.password = "";
            }
        }
        else
        {
            model.validateWay = "";
            model.userName = "";
            model.password = "";
        }

        //string xmlPath = Server.MapPath(Whir.Common.Settings.GetString("XmlPath") + "EmailConfig.xml");
        string xmlPath = Server.MapPath("~/" + Whir.Common.Settings.GetString("XmlPath") + "EmailConfig.xml");
        Type type = typeof(Whir.Config.Model.EmailConfig);
        Whir.Common.XmlHelper.SerializerObject(xmlPath, type, model);               //序列化

        this.OLAdd();    //操作日志
    }

    //操作日志
    protected void OLAdd()
    {
        Whir.Log.Model.Whir_OperateLog model = new Whir.Log.Model.Whir_OperateLog();
        model.title = "邮箱配置";
        model.operateType = 2;      //修改
        model.operateResult = true;
        model.operateDesc = "修改邮箱配置";

        Whir.Log.OperateLog.Add(model);
    }

    public bool Send(string to)
    {
        try
        {
            //string xmlPath = Server.MapPath(Whir.Common.Settings.GetString("XmlPath") + "EmailConfig.xml");
            string xmlPath = Server.MapPath("~/" + Whir.Common.Settings.GetString("XmlPath") + "EmailConfig.xml");
            Type type = typeof(Whir.Config.Model.EmailConfig);
            Whir.Config.Model.EmailConfig model = (Whir.Config.Model.EmailConfig)Whir.Common.XmlHelper.DeserializeObject(xmlPath, type);         //反序列化

            MailMessage msg = new MailMessage();
            msg.From = model.email;
            msg.To = to;
            msg.BodyFormat = MailFormat.Html;
            msg.BodyEncoding = System.Text.Encoding.Default;
            msg.Subject = "这是一封测试邮件";
            msg.Body = "这是一封测试邮件";
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", model.userName);//发信人的用户名
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", model.password);//发信人的密码
            SmtpMail.SmtpServer = model.SMTP;
            SmtpMail.Send(msg);
            return true;
        }
        catch
        {
            return false;
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        bool flag = false;
        flag = Send(txtEmailCs.Text.Trim());
        if (flag)
        { JsHelper.Alert("发送成功"); }
        else
        { JsHelper.Alert("发送失败，请检查网站系统邮箱配置"); }
    }
}
